var guanpai = require("guanpai");

cc.Class({
    extends: cc.Component,

    properties: {
        registerPanel: cc.Node,
        usernameEditBox: cc.EditBox,
        passwordEditBox: cc.EditBox,
    },

    // use this for initialization
    onLoad: function () {
    },

    switchToRegisterPanel: function () {
        this.registerPanel.active = true;
        this.node.active = false;
    },

    onClickLogin: function () {
        var username = this.usernameEditBox.string;
        var password = this.passwordEditBox.string;
        if (username.length <= 0) {
            guanpai.ui.createScreenMsg("用户名不能为空");
        }
        else if (password.length <= 0) {
            guanpai.ui.createScreenMsg("密码不能为空");
        }
        else {
            guanpai.netEngine.send("login", {username: username, password: password}, (reply)=>{
                if (!reply.errorCode) { // 登陆成功
                    guanpai.userData = reply.userData;
                    cc.log("userData: " + JSON.stringify(guanpai.userData));
                    guanpai.ui.createScreenMsg("用户" + guanpai.userData.id + "登陆成功");
                    cc.director.loadScene("scenes/Main");
                }
                else {  // 登陆失败
                    guanpai.ui.createScreenMsg("用户不存在或密码错误");
                }
            });
        }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
